﻿using PHMEE.Constants;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PHMEE.EnergyEfficiency
{
    public class FlowmeterMapping
    {
        /// <summary>
        /// 设备类型（主机、副机、锅炉等）
        /// </summary>
        public DeviceFuelEnum DeviceName { get; set; }

        /// <summary>
        /// 进油流量计名称列表
        /// </summary>
        public List<string> InletKeys { get; set; } = new();

        /// <summary>
        /// 回油流量计名称列表
        /// </summary>
        public List<string> OutletKeys { get; set; } = new();

        /// <summary>
        /// 排除的进油流量计名称列表（仅在合并模式中使用）
        /// </summary>
        public List<string> ExcludeInletKeys { get; set; } = new();

        /// <summary>
        /// 排除的回油流量计名称列表（仅在合并模式中使用）
        /// </summary>
        public List<string> ExcludeOutletKeys { get; set; } = new();

        /// <summary>
        /// 计算瞬时油耗
        /// </summary>
        /// <param name="flowmeterGroups">按流量计名称分组的流量计数据</param>
        /// <returns>瞬时油耗值</returns>
        public float CalculateInstantOil(Dictionary<string, List<MassFlowmeter>> flowmeterGroups)
        {
            float inletInstant = 0, outletInstant = 0;

            // 计算进油瞬时值
            foreach (var inletKey in InletKeys)
            {
                if (flowmeterGroups.ContainsKey(inletKey))
                {
                    inletInstant += flowmeterGroups[inletKey].FirstOrDefault()?.Instant ?? 0f;
                }
            }

            // 计算回油瞬时值
            foreach (var outletKey in OutletKeys)
            {
                if (flowmeterGroups.ContainsKey(outletKey))
                {
                    outletInstant += flowmeterGroups[outletKey].FirstOrDefault()?.Instant ?? 0f;
                }
            }

            // 排除的进油瞬时值
            foreach (var excludeInletKey in ExcludeInletKeys)
            {
                if (flowmeterGroups.ContainsKey(excludeInletKey))
                {
                    inletInstant -= flowmeterGroups[excludeInletKey].FirstOrDefault()?.Instant ?? 0f;
                }
            }

            // 排除的回油瞬时值
            foreach (var excludeOutletKey in ExcludeOutletKeys)
            {
                if (flowmeterGroups.ContainsKey(excludeOutletKey))
                {
                    outletInstant -= flowmeterGroups[excludeOutletKey].FirstOrDefault()?.Instant ?? 0f;
                }
            }

            // 返回净瞬时油耗
            return inletInstant - outletInstant;
        }
    }
}
